package com.hilihili.service.impl;

import com.hilihili.domain.dto.FavoritesVideoDTO;
import com.hilihili.domain.dto.FavoritesVideoListDTO;
import com.hilihili.domain.entity.FavoritesVideoList;
import com.hilihili.mapper.FavoritesVideoListMapper;
import com.hilihili.mapper.VideoMapper;
import com.hilihili.service.IFavoritesVideoListService;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FavoritesVideoListServiceImpl implements IFavoritesVideoListService
{

    @Autowired
    private FavoritesVideoListMapper favoritesVideoListMapper;
    @Autowired
    private VideoMapper videoMapper;

    @Override
    public List<FavoritesVideoDTO> selectList(FavoritesVideoList favoritesVideoList) {
        return favoritesVideoListMapper.selectList(favoritesVideoList);
    }

    @Override
    public int insertList(FavoritesVideoList favoritesVideoList) {
        return favoritesVideoListMapper.insertList(favoritesVideoList);
    }

    @Override
    public int selectCount(Long favoritesId) {
        return favoritesVideoListMapper.selectCount(favoritesId);
    }

    @Override
    public List<FavoritesVideoList> selectListByUserIdAndVideoId(Long userId, Long videoId) {
        //todo BUG   Unknown column 'create_at' in 'where clause'
        return favoritesVideoListMapper.selectListByUserIdAndVideoId(userId,videoId);
    }

    @Override
    public void insertBatch(List<FavoritesVideoList> favoritesList) {
        favoritesVideoListMapper.insertBatch(favoritesList);
    }



    @Transactional
    @Override
    public void addFavoritesVideoList(FavoritesVideoListDTO favoritesVideoListDTO) {
        Long videoId = favoritesVideoListDTO.getVideoId();
        Long userId = SecurityUtils.getUserId();

        if ("".equals(favoritesVideoListDTO.getFavoritesCheckedOldList())){
            // 批量插入数据
            String[] split = favoritesVideoListDTO.getFavoritesCheckedList().split(",");
            List<Long> newIds = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                newIds.add(Long.parseLong(split[i]));
            }
            commonInsert(newIds,videoId,userId);
            // 增加或修改该视频的收藏数
            videoMapper.updateStarsSubtract(videoId,Long.parseLong("1"));
        }else if("".equals(favoritesVideoListDTO.getFavoritesCheckedList())){
            // 批量删除数据
            String[] splitOld = favoritesVideoListDTO.getFavoritesCheckedOldList().split(",");
            List<Long> oldIds = new ArrayList<>();
            for (int i = 0; i < splitOld.length; i++) {
                oldIds.add(Long.parseLong(splitOld[i]));
            }
            favoritesVideoListMapper.deleteByIds(oldIds,videoId,userId);
            videoMapper.updateStarsSubtract(videoId,Long.parseLong("-1"));
        }else {// 两者都不为空
            String[] split = favoritesVideoListDTO.getFavoritesCheckedList().split(",");
            List<Long> newIds = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                newIds.add(Long.parseLong(split[i]));
            }
            String[] splitOld = favoritesVideoListDTO.getFavoritesCheckedOldList().split(",");
            List<Long> oldIds = new ArrayList<>();
            for (int i = 0; i < splitOld.length; i++) {
                oldIds.add(Long.parseLong(splitOld[i]));
            }
            List<Long> common = new ArrayList<>();
            newIds.forEach(item -> {
                oldIds.forEach(obj -> {
                    if (item.equals(obj)){
                        common.add(item);
                    }
                });
            });
            if (oldIds.size() == common.size()){//本来有，现在又多加个收藏夹
                // 去掉已经存在的id
                List<Long> newIdsTemp = new ArrayList<>();
                for (Long newId : newIds) {
                    newIdsTemp.add(newId);
                }
                newIdsTemp.removeAll(oldIds);
                // 批量插入数据
                commonInsert(newIdsTemp,videoId,userId);
            }else {
                if (common.size() == 0){
                    // 取消oldids收藏夹
                    favoritesVideoListMapper.deleteByIds(oldIds,videoId,userId);
                    // 插入newids// 批量插入数据
                    commonInsert(newIds,videoId,userId);
                }else if(common.size() == newIds.size()){
                    List<Long> oldIdsTemp = new ArrayList<>();
                    // 取出不需要删除的收藏夹ids
                    for (Long oldId : oldIds) {
                        oldIdsTemp.add(oldId);
                    }
                    oldIdsTemp.removeAll(newIds);
                    favoritesVideoListMapper.deleteByIds(oldIdsTemp,videoId,userId);
                }else {
                    List<Long> oldIdsTemp = new ArrayList<>();
                    for (Long oldId : oldIds) {
                        oldIdsTemp.add(oldId);
                    }
                    oldIdsTemp.removeAll(newIds);
                    favoritesVideoListMapper.deleteByIds(oldIdsTemp,videoId,userId);

                    List<Long> newIdsTemp = new ArrayList<>();
                    for (Long newId : newIds) {
                        newIdsTemp.add(newId);
                    }
                    newIdsTemp.removeAll(oldIds);
                    commonInsert(newIdsTemp,videoId,userId);
                }
            }
        }
    }

    public void commonInsert(List<Long> newIds,Long videoId,Long userId){
        // 批量插入数据
        List<FavoritesVideoList> favoritesVideoLists = new ArrayList<>();
        newIds.forEach(item -> {
            FavoritesVideoList ff = new FavoritesVideoList();
            ff.setFavoritesId(item);
            ff.setVideoId(videoId);
            ff.setCreateTime(new Date());
            ff.setCreateAt(userId);
            favoritesVideoLists.add(ff);
        });
        favoritesVideoListMapper.insertBatch(favoritesVideoLists);
    }

}
